package com.heer.gymnasium;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.heer.api.ApiService;
import com.heer.api.Http;
import com.heer.api.sign.SignHelper;
import com.heer.gymnasium.service.MessageService;
import com.heer.gymnasium.util.Session;
/*import com.tencent.bugly.crashreport.CrashReport;*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import butterknife.ButterKnife;

/**
 * Created by heer on 16/6/5.
 */
public class AppHelper extends MultiDexApplication {
    static Context _context;
    static Resources _resource;
    static ApiService apiService;


    @Override
    public void onCreate() {
        super.onCreate();
        _context = getApplicationContext();
        _resource = _context.getResources();
        apiService = Http.getService();
        ButterKnife.setDebug(true);



        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
       /* CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));*/
        // 初始化Bugly
      /*  CrashReport.initCrashReport(context, "5ddc33dc41", true, strategy);*/
        if (!TextUtils.isEmpty(Session.getSessionKey())) {
            SignHelper.initToken(Session.getSessionKey());
        }


    }

    public static synchronized AppHelper context() {
        return (AppHelper) _context;
    }

    public ApiService apiService() {
        return apiService;
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

}
